<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        .preview-container{
            width: 1226px;
            margin:50px auto;
            display: flex;
        }
        .preview-container .preview-wapper{
            width: 400px;
            margin-right: 20px;
            position: relative;
        }
        .preview-container .preview-wapper .preview{
            width: 400px;
            height: 400px;
            background-color: orange;
            position: relative;
        }
        .preview-container .preview-wapper .preview{
            width: 100%;
        }
        .preview-container .preview-wapper .preview .shadow{
            width: 200px;
            height: 200px;
            /* background-color: rgba(0,0,0,0.5); */
            background: url(//img-tmdetail.alicdn.com/tps/i4/T12pdtXaldXXXXXXXX-2-2.png);
            position: absolute;
            left: 0;
            top: 0;
            display: none;
        }
        .preview-container .preview-info{
            flex: 1;
            /* background-color: aquamarine; */
        }
        .preview-scale{
            width: 400px;
            height: 400px;
            background-color: rgba(125,255,255,0.5);
            position: absolute;
            left: 420px;
            top: 0;
            display: none;
            /* overflow: hidden; */
        }
        .preview-scale img{
            width: 800px;
            height: 800px;
            position: absolute;
            left: 0;
            top: 0;
            z-index: -1;
        }
    </style>
</head>
<body>
    <div class="preview-container">
        <div class="preview-wapper">
            <div class="preview">
                <img class="smallImg" src="../images/small_1.jpg" alt="">
                <div class="shadow"></div>
            </div>
            <div class="preview-scale">
                <img class="bigImg" src="../images/big_1.jpg" alt="">
            </div>
            <ul class="preview-list">
                <li><img src="" alt=""></li>
                <li><img src="" alt=""></li>
                <li><img src="" alt=""></li>
                <li><img src="" alt=""></li>
            </ul>
        </div>
        <div class="preview-info">

        </div>
    </div>  
</body>
<script>

    /* 
    放大镜的实现步骤
    1. 等比例放大  => 布局
        shadow           smallImg           shadowx
    ——————————————  = ——————————————    = ————————————   
     previewScale         bigImg              bigX
   
       200        400
    ————————  = ———————— 
       400       bigImg => 800

    2. 左边移动的是shadow  右边移动的是 bigImg  => 布局

    3. shadow 和 bigImg 移动的距离也是等比例且反向的
    
    
    */


    var previewWapper = document.getElementsByClassName("preview-wapper")[0];
    var preview = document.getElementsByClassName("preview")[0];
    var shadow = document.getElementsByClassName("shadow")[0];
    var bigImg = document.getElementsByClassName("bigImg")[0];
    var smallImg = document.getElementsByClassName("smallImg")[0];
    var previewScale = document.getElementsByClassName("preview-scale")[0];

    // 问题:
    // 此处代码页面加载时执行  => 此时shadow 和 bigImg 都是隐藏的 => clientWidth clientHeight 对于隐藏的元素不生效  => 0
    var maxLeft = preview.clientWidth - shadow.clientWidth;
    var maxTop = preview.clientHeight - shadow.clientHeight;

    console.log(shadow.clientWidth,shadow.clientHeight,bigImg.clientWidth);

    var scale = bigImg.clientWidth / smallImg.clientWidth;

    preview.onmouseenter = function(){
        shadow.style.display = "block";
        previewScale.style.display = "block";
    }
    preview.onmouseleave = function(){
        shadow.style.display = "none";
        previewScale.style.display = "none";
    }
    preview.onmousemove = function(e){
        var e = e || window.event;
        var x = e.pageX - previewWapper.offsetLeft - shadow.clientWidth / 2;
        var y = e.pageY - previewWapper.offsetTop - shadow.clientHeight / 2;

        x = Math.min(Math.max(0, x), maxLeft);
        y = Math.min(Math.max(0, y), maxTop);

        shadow.style.left = x + "px";
        shadow.style.top = y + "px";

        bigImg.style.left = -scale * x + "px"
        bigImg.style.top = -scale * y + "px"

    }

</script>
</html>